package cn.apestar.modules.sms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.message.BasicNameValuePair;
import com.gexin.rp.sdk.base.uitls.MD5Util;
import cn.apestar.common.config.Global;
import cn.apestar.common.exception.BaseException;
import cn.apestar.common.utils.StringUtils;

/**
 * 短信发送
 * 
 * @author lvh
 */
public class SmsUtil {

    /**
     * 发送短信
     */
    public static Map sendSms(String phone, String content) {

        String url = "http://api.smsbao.com/sms?u={USERNAME}&p={PASSWORD}&m={PHONE}";

        boolean flag = true;
        String message = "";
        Map mapResult = new HashMap();
        if (StringUtils.isEmpty(phone)) {
            throw new BaseException("电话不能为空！");
        } else
            url = url.replace("{PHONE}", phone);

        if (StringUtils.isEmpty(content)) {
            message = "";
            throw new BaseException("发送内容不能为空！");
        }
        // else
        // url = url.replace("{CONTENT}", content);

        String USERNAME = Global.getConfig("SMS_NAME");

        String PASSWORD = Global.getConfig("SMS_PASSWORD"); // "e10adc3949ba59abbe56e057f20f883e";

        url = url.replace("{USERNAME}", USERNAME);

        url = url.replace("{PASSWORD}", MD5Util.getMD5Format(PASSWORD));
        try {
            if (flag) {
                List<BasicNameValuePair> formparams = new ArrayList();
                BasicNameValuePair p = new BasicNameValuePair("c", content);
                formparams.add(p);
                String s = HttpUtil.ExecutionPost("http://7xqa78.com1.z0.glb.clouddn.com/1460001521846file1?odconv/jpg/info", formparams);
                if ("0".equals(s)) {
                    message = "发送成功！";
                    flag = true;
                } else {
                    throw new BaseException("发送失败,请检查电话号码！");
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            flag = false;
            message = e.getMessage();
        }
        finally {
            mapResult.put("flag", flag);
            mapResult.put("message", message);
            return mapResult;
        }
    }

    public static void main(String[] args) {

        SmsUtil.sendSms("18050086692", "TEST123456");
    }
}
